package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.*;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.service.SeBillAbnormalService;
import com.sumainfor.pc.gmio.service.SeBillPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/*
*账单异常/违约金异常
 */
@Service
public class SeBillAbnormalSerivceImpl implements SeBillAbnormalService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(SeBillAbnormalSerivceImpl.class);
    @Autowired
    private SeBillAbnormalMapper seBillAbnormalMapper;
    /**
     新增账单异常/违约金异常记录
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult addSeBill(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {

                if(params.get("BillType")==null || params.get("Source")==null || params.get("HouId")==null
                ||params.get("HouInfoId")==null || params.get("ifOverflow")==null){
                    result.putFailed("参数不正确");
                }
                //获取当前操作人的服务中心编号
                SysUserVo userVo = ContextUtils.getSysUser();
                ParentDeptVo vo = null;
                for (int i = 0; i < userVo.getLength(); i++) {
                    ParentDeptVo vo1 = userVo.getParentDeptVo();
                    if (vo1.getType().equals(3)) {
                        vo = vo1;
                        break;
                    }
                }
                if (vo!=null){
                    params.put("MiId",vo.getDeptId());//服务中心编号
                }
                params.put("SysUserId",ContextUtils.getUserId());//操作人编号
                params.put("Abnor",0);//违约金异常类型  0无状态  1.租客违约   2.唐巢违约\n只有退租账单才有该编号
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "2");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                Integer Count=seBillAbnormalMapper.addSeBill(params);
                if (Count>0){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
